<!DOCTYPE html>
<html ng-app="demo">
    <head>
        <meta charset="utf-8">
        <title>聊天</title>

    <link rel="stylesheet" type="text/css" href="__PUBLIC__/im/css/RongIMWidget.css"/>

    <script src="__PUBLIC__/im/vendor/jquery-2.2.2.js"></script>
    <script src="__PUBLIC__/im/vendor/angular-1.4.8.js"></script>

    <!-- 融云IMLib -->
    <script src="//cdn.ronghub.com/RongIMLib-2.2.8.min.js"></script>
    <script src="//cdn.ronghub.com/RongEmoji-2.2.5.min.js"></script>
    <script src="//cdn.ronghub.com/Libamr-2.2.5.min.js"></script>
    <script src="//cdn.ronghub.com/RongIMVoice-2.2.5.min.js"></script>

    <!-- 上传插件 -->
    <script src="__PUBLIC__/im/vendor/plupload.full.min-2.1.1.js"></script>
    <script src="__PUBLIC__/im/vendor/qiniu-1.0.17.js"></script>

    <!-- 增强体验插件 -->
    <script src="__PUBLIC__/im/vendor/jqueryrebox/jquery-rebox-0.1.0.js"></script>

    <!-- IM插件 -->
    <script type="text/javascript" src="__PUBLIC__/im/js/RongIMWidget.js"></script>
    </head>
<body ng-controller="main">

<rong-widget></rong-widget>

</body>
<script type="text/javascript">
(function () {
/*
    将相同代码拆出来方便维护
 */
window.RongDemo = {
    common: function (WebIMWidget, config, $scope) {
        WebIMWidget.init(config);
        WebIMWidget.setUserInfoProvider(function(targetId, obj) {
            obj.onSuccess({
                name: "用户：" + targetId
            });
        });

        WebIMWidget.setGroupInfoProvider(function(targetId, obj){
            obj.onSuccess({
                name:'群组1：' + targetId
            });
        })

        $scope.setconversation = function () {
            if (!!$scope.targetId) {
               WebIMWidget.setConversation(Number($scope.targetType), $scope.targetId, "用户：" + $scope.targetId);
                WebIMWidget.show();
            }
        };

        $scope.customerserviceId = "KEFU151123424798550";
        $scope.setcustomerservice = function () {
            WebIMWidget.setConversation(Number(RongIMLib.ConversationType.CUSTOMER_SERVICE), $scope.customerserviceId);
            WebIMWidget.show();
        }

        $scope.show = function() {
            WebIMWidget.show();
        };

        $scope.hidden = function() {
            WebIMWidget.hidden();
        };

        WebIMWidget.show();
    }
}

})()
var demo = angular.module("demo", ["RongWebIMWidget"]);

demo.controller("main", ["$scope", "WebIMWidget", "$http", function($scope, WebIMWidget, $http) {

    $scope.targetType = 1; //1：私聊 更多会话类型查看http://www.rongcloud.cn/docs/api/js/global.html#ConversationType
    $scope.targetId = '{$_SESSION["user"]["id"]}';

    //注意实际应用中 appkey 、 token 使用自己从融云服务器注册的。
    var config = {
       appkey: '{$rong_key}',
        token: "{$rong_token}",
        displayConversationList: true,
        style:{
            left:3,
            bottom:3,
            width:430
        },
        onSuccess: function(id) {
            $scope.user = id;
            document.title = '用户：' + id;
            console.log('连接成功：' + id);
        },
        onError: function(error) {
            console.log('连接失败：' + error);
        }
    };
    RongDemo.common(WebIMWidget, config, $scope);

        // 示例：获取 userinfo.json 中数据，根据 targetId 获取对应用户信息
         WebIMWidget.setUserInfoProvider(function(targetId,obj){
            $http({
              url:"/Home/im/get_user_info"
            }).success(function(rep){
              var user;
              rep.userlist.forEach(function(item){
                if(item.id==targetId){
                  user=item;
                }
              })
              if(user){
                obj.onSuccess({id:user.id,name:user.name,portraitUri:user.portraitUri});
              }else{
                obj.onSuccess({id:targetId,name:"用户："+targetId});
              }
            })
         });


          WebIMWidget.setOnlineStatusProvider(function(arr, obj) {
            $http({
                url: "/Home/im/online"
            }).success(function(rep) {
                obj.onSuccess(rep.data);
            })
        });

}]);
  // demo.controller("main", ["$scope", "RongCustomerService", function($scope,RongCustomerService) {
  //     RongCustomerService.init({
  //      appkey: '{$rong_key}',
  //       token: "{$rong_token}",
  //       customerServiceId:"****",
  //     });
  //   }]);

</script>

</html>